//
//  ColumnDrugListViewController.m
//  MedicineBox
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ColumnDrugListViewController.h"
#import "ColumnDrugCollectionViewCell.h"
#import "AsyncConnection.h"
#import "DrugDetailViewController.h"
#import "HomeModel.h"

#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"
@interface ColumnDrugListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collectView;
@property (nonatomic, retain) UILabel *textColor;
@property (nonatomic, retain) UIScrollView *scrollView;
@end

CGFloat width;

@implementation ColumnDrugListViewController

-(void)dealloc
{
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"KNOTIFIACTION" object:nil];
    [_cArray release];
    [_scrollView release];
    [_textColor release];
    [_collectView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = [UIColor whiteColor];
     self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self createNavigation];
    [self createScrollView];
    [self createCollectView];
    [self createNotification];
}
#pragma mark - 
#pragma mark 消息中心
- (void)createNotification
{
    //创建消息中心,注册消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifiAction:) name:@"KNOTIFIACTION" object:nil];
}

- (void)notifiAction:(NSNotification *)noti
{
    NSDictionary *dic = noti.userInfo;
    NSArray *arr = [NSArray array];
    arr = [dic objectForKey:@"array"];
    NSInteger item = [[dic objectForKey:@"item"] integerValue];
    HomeModel *model = [arr objectAtIndex:item];
    
    
    DrugDetailViewController *detail = [[DrugDetailViewController alloc] init];

    detail.deatilId = @([model._id integerValue]);
    detail.namecn = model.namecn;
    detail.score = model.score;
    [self presentViewController:detail animated:YES completion:nil];
    [detail release];
}

#pragma mark -
#pragma mark 设置导航栏
- (void)createNavigation
{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    navView .backgroundColor = [UIColor colorWithRed:86/255.0 green:137/255.0 blue:156/255.0 alpha:1];
    navView.nightBackgroundColor = UIColorFromRGB(0x444444);
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 63, self.view.frame.size.width, 0.5)];
    line.backgroundColor = [UIColor whiteColor];
    [navView addSubview:line];
    [self.view addSubview:navView];
    [navView release];
    

//标题
    

    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2, 30, 100, 30)];
    titleLabel.text = @"分类";
    titleLabel.font = [UIFont fontWithName:@"Baskerville-SemiBold" size:20];

    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.nightTextColor = [UIColor whiteColor];
    [navView addSubview:titleLabel];
 //返回
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10 , 30, 60, 30)];
    [navView addSubview:backButton];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton release];
}

- (void)buttonAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 
#pragma mark 创建ScrollView
- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 36)];
//    self.scrollView.backgroundColor = [UIColor colorWithRed:56/255.0 green:185/255.0 blue:94 / 255.0 alpha:0.5];
    self.scrollView.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self.view addSubview:self.scrollView];
    for (int i = 0; i < self.cArray.count; i++) {
        NSString *text = [[self.cArray objectAtIndex:i] objectForKey:@"cName"];
        UILabel *cNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 90 + 5, 10, 80, 20)];
        cNameLabel.text = text;
        cNameLabel.textAlignment = NSTextAlignmentCenter;
        cNameLabel.textColor = [UIColor grayColor];
        cNameLabel.font = [UIFont systemFontOfSize:15];
        cNameLabel.nightTextColor = [UIColor whiteColor];
        [self.scrollView addSubview:cNameLabel];
        cNameLabel.userInteractionEnabled = YES;
        cNameLabel.tag = i + 1;
        if (i == 0) {
            cNameLabel.textColor = [UIColor redColor];
            self.textColor = cNameLabel;

        }
        self.scrollView.contentSize = CGSizeMake(100 * self.cArray.count, 0);

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [cNameLabel addGestureRecognizer:tap];
        [tap release];
        [cNameLabel release];
     }
    [_scrollView release];
}


#pragma mark -
#pragma mark 创建collectView
- (void)createCollectView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100) collectionViewLayout:layout];
    [self.view addSubview:self.collectView];
    self.collectView.backgroundColor = [UIColor clearColor];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    
    self.collectView.pagingEnabled = YES;
    self.collectView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    self.collectView.contentSize = CGSizeMake(self.collectView.frame.size.width * self.cArray.count, 0);
    self.collectView.nightBackgroundColor = UIColorFromRGB(0x343434);
    
    [self.collectView registerClass:[ColumnDrugCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectView release];
    [layout release];
}

#pragma mark - 
#pragma mark label条与collect滚动
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    UILabel *lable = (UILabel *)tap.view;
    [self createTempLable:lable];
}
- (void)createTempLable:(UILabel *)lable
{
    self.textColor.textColor = [UIColor grayColor];
    self.textColor = lable;
    if (lable.textColor != [UIColor redColor]) {
        lable.textColor = [UIColor redColor];
    }

    self.collectView.contentOffset = CGPointMake((lable.tag - 1) * self.collectView.frame.size.width , 0);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.collectView) {
        NSInteger i = scrollView.contentOffset.x / scrollView.frame.size.width;
        UILabel *label = (UILabel *)[self.view viewWithTag:i + 1];
        self.scrollView.contentOffset = CGPointMake(60 * i, 0);
        [self createTempLable:label];
    }
}

#pragma mark - 
#pragma mark 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.cArray.count;
}
#pragma mark - 
#pragma mark 每个分区的item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark -
#pragma mark 自定义cell (是collectView)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ColumnDrugCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
   
    [cell valueWithUrl:[[self.cArray objectAtIndex:indexPath.section] objectForKey:@"cUrl"]];
    
    return cell;
}
#pragma mark - 
#pragma mark 每个item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.frame.size.width, collectionView.frame.size.height);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
